package com.xiaoyy.sso.core.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import com.xiaoyy.sso.common.vo.UserVO;
import com.xiaoyy.sso.core.dao.model.SysUser;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.Charset;

public class UserSecureUtil {
    private static String secureKey="eGlhb3l5TG92ZXpoQW5kSA==";

    public static String aesEncrypt(String str){
        return  StringUtils.isBlank(str)?str:SecureUtil.aes(Base64.decode(secureKey)).encryptBase64(str,Charset.forName("UTF-8"));
    }

    public  static String aesDecrypt(String str){
        return StringUtils.isBlank(str)?str:SecureUtil.aes(Base64.decode(secureKey)).decryptStr(str, Charset.forName("UTF-8"));
    }
    public static String enSysUser(SysUser user) {
        String userId = "";
        String userName = "";
        String userPassword = "";
        String email = "";
        String cellphone = "";
        String status = "";
        String userType = "";
        String fullName = "";
        String idcard = "";
        if (user.getUserId() != null) {
            userId = user.getUserId();
        }
        if (user.getUserName() != null) {
            userName = user.getUserName();
        }
        if (user.getUserPassword() != null) {
            userPassword = user.getUserPassword();
        }
        if (user.getEmail() != null) {
            email = user.getEmail();
        }
        if (user.getCellphone() != null) {
            cellphone = user.getCellphone();
        }
        if (user.getStatus() != null) {
            status = user.getStatus();
        }
        if (user.getUserType() != null) {
            userType = user.getUserType();
        }
        if (user.getFullName() != null) {
            fullName = user.getFullName();
        }
        if (user.getIdcard() != null) {
            idcard = user.getIdcard();
        }

        String encodeRules = userId + "~|~" + userName + "~|~" + userPassword + "~|~"
                + email + "~|~" + cellphone + "~|~" + status + "~|~"
                + userType + "~|~" + fullName + "~|~" + idcard;
        String hashcode = aesEncrypt(encodeRules);
        return hashcode;
    }

    public static SysUser deSysUser(String str) {
        SysUser sysUser = new SysUser();
        String deStr = aesDecrypt(str);
        String[] strList = StringUtils.split(deStr, "~|~");
        if (strList != null) {
            sysUser.setUserId(strList[0]);
            sysUser.setUserName(strList[1]);
            sysUser.setUserPassword(strList[2]);
            sysUser.setEmail(strList[3]);
            sysUser.setCellphone(strList[4]);
            sysUser.setStatus(strList[5]);
            sysUser.setUserType(strList[6]);
            sysUser.setFullName(strList[7]);
            sysUser.setIdcard(strList[8]);
        }
        return sysUser;
    }
    /**
     * 用户加密字段解密
     */
    public static void aesDecryptUser(UserVO userVO) {
        //手机号码解密
        if (StringUtils.isNotBlank(userVO.getCellphone())) {
            userVO.setCellphone(aesDecrypt(userVO.getCellphone()));
        }
        //邮箱解密
        if (StringUtils.isNotBlank(userVO.getEmail())) {
            userVO.setEmail(aesDecrypt(userVO.getEmail()));
        }
        //身份证解密
        if (StringUtils.isNotBlank(userVO.getIdcard())) {
            userVO.setIdcard(aesDecrypt(userVO.getIdcard()));
        }
    }
    /**
     * password 生成工具类
     */
    public static String createPassword(String userId,String password){
        return SecureUtil.md5(userId + password);
    }
}
